<template>
  <div class="header">
    <div class="header-inner">
      <div class="logo">闪购商城</div>
      <div class="nav-right">
        <el-button 
          type="primary" 
          plain 
          :class="{ 'active-btn': activeTab === 'home' }" 
          @click="handleTabClick('home', '/home')"
        >
          首页
        </el-button>
        <el-button 
          type="primary" 
          plain 
          :class="{ 'active-btn': activeTab === 'cart' }" 
          @click="handleTabClick('cart', '/cart')"
        >
          购物车
        </el-button>
        <span class="welcome">欢迎：{{ userStore.username || '访客' }}</span>
        <el-button 
          type="danger" 
          plain 
          @click="handleLogout"
        >
          退出登录
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user' 
import { ref } from 'vue'

const router = useRouter()
const userStore = useUserStore()
// 标记当前激活的按钮
const activeTab = ref('') 

const handleTabClick = (tab, path) => {
  activeTab.value = tab
  router.push(path)
}

const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出登录成功')
  router.push('/login')
}
</script>

<style scoped>
.header {
  background-color: #fff;
  border-bottom: 1px solid #ccc; 
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px; 
}
.logo {
  font-size: 20px;
  font-weight: bold;
  color: #f43f3b; 
}
.nav-right {
  display: flex;
  align-items: center;
}
.welcome {
  margin: 0 10px;
  color: #333;
}

/* 按钮默认样式（浅灰色） */
.el-button--primary.plain {
  background-color: #f0f0f0 ;
  border-color: #ccc;
  color: #666 ;
}

.active-btn {
  background-color: #f43f3b;
  border-color: #f43f3b ;
  color: #fff ;
}

.el-button--danger.plain {
  background-color: #f43f3b;
  border-color: #f43f3b;
  color: #fff;
}

.el-button--primary.plain:hover {
  opacity: 0.9; 
}
.el-button--danger.plain:hover {
  opacity: 0.9; 
}
</style>